/**
 * Promise.withResolvers Polyfill
 * 为不支持 ES2024 Promise.withResolvers 的环境提供兼容性支持
 * 
 * 参考: https://github.com/tc39/proposal-promise-with-resolvers
 */

// 检查是否已经存在该方法，如果不存在则添加 polyfill
if (typeof Promise.withResolvers === 'undefined') {
  // @ts-ignore - 为全局 Promise 对象添加新方法
  Promise.withResolvers = function <T>() {
    let resolve: (value: T | PromiseLike<T>) => void;
    let reject: (reason?: any) => void;
    
    const promise = new Promise<T>((res, rej) => {
      resolve = res;
      reject = rej;
    });
    
    return {
      promise,
      resolve: resolve!,
      reject: reject!
    };
  };
}

// 为 TypeScript 提供类型定义
declare global {
  interface PromiseConstructor {
    withResolvers<T>(): {
      promise: Promise<T>;
      resolve: (value: T | PromiseLike<T>) => void;
      reject: (reason?: any) => void;
    };
  }
}

export {};

